﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Exceptions;
using Interfaces;
using System.Data;

namespace Controllers
{
    public class FacadeController
    {
        DBconnection DBC;

        public FacadeController()
        {
            DBC = new DBconnection();            
        }
        
        
        /// <summary>
        /// Indsætter alle data fra Kategori Xmlfilen til SQL serveren
        /// Det nedenstående er skrevet af Lars Jacobsen og Anders Madsen, de steder hvor Lars S Jacobsen
        /// har Anders ikke været en del af.
        /// </summary>
        /// <param name="path"></param>
        public void ImportCatageoryDataFromERP(string path) // denne metode er skrevet af Lars S Jacobsen
        {            
                List<Category> CategoryList = XmlParser.importCategoryData(path);

                   // Valider Indata
                if (CategoryList == null)
                {
                    throw new Exception("Der er sket en fejl under importering af CatagoriData");
                }
            
                foreach (Category cat in CategoryList)
                {
                  // Smid data ind i databasen
                   // try
                    //{
                        DBC.Add_Data_Category(cat.Level, cat.Name, cat.Description, cat.BrandCode, cat.ID.ToString());
                    //}
                    //catch (Exception ex)
                    //{
                      //  continue;
                    //}
                }
        }
        /// <summary>
        /// Importerer Produkt Data fra produkt XML filen til SQL Serveren
        /// </summary>
        /// <param name="path"></param>
        /// <param name="isActive"></param>
        public void ImportProducktDataFromERP(string path) // Denne metode er skrevet af Lars S Jacobsen.
        {
            DBconnection connector = new DBconnection();

            List<Product> ProducktList = XmlParser.importProduktdata(path);

            if (ProducktList == null)
            {
                throw new Exception("");
            }
            
            foreach (Product prod in ProducktList)
            {
                //try
                //{
                    DBC.Add_Data_Product(prod.ProductNumber, prod.BrandCode, prod.AvailabilityLevel, prod.Category, prod.ProductStatus, prod.Price, prod.LaunchDate);
                //}
                //catch (Exception ex)
                //{
                  //  continue;
                //}
            }
        }
        /// <summary>
        /// IMporterer Data fra Brand XML filen til SQL Serveren
        /// </summary>
        /// <param name="path"></param>       
        public void ImportBrandDataFromERP(string path)
        {
            DBconnection connector = new DBconnection();
            List<Brand> Brandliste = XmlParser.ImportBrandData(path);

            if (Brandliste == null)
            {
                throw new Exception("Der skete en fejl under importering fra XML");
            }

            foreach(Brand brand in Brandliste)
            {
               // try
               // {
                    DBC.Add_Data_Brand(brand.BrandCode, brand.BrandName);
               // }
               // catch (Exception ex)
              //  {
              //      continue;
              //  }

            }
        } //Denne metode er skrevet af lars S Jacobsen
        /// <summary>
        /// Importerer ProduktDetaljer fra ERP Systemet.
        /// </summary>
        /// <param name="path"></param>
        public void ImportProductDetailsFromERP(string path)
        {
            List<ProductDetail> ProduktDetaljer = XmlParser.ImportProduktDetailData(path);
            DBconnection connector = new DBconnection();

            if (ProduktDetaljer == null)
            {
                throw new Exception("Der er sket en fejl under importering af Produktdetaljerne");
            }

            foreach (ProductDetail proddetail in ProduktDetaljer)
            {
                DBC.Add_Data_ProductDetail(proddetail.Language, proddetail.Description, proddetail.ReplacementsNotes, proddetail.ProductNumber);
            }
        } // Denne metode er skrevet af Lars S Jacobsen.
        
        public void Data_To_Brand(string brandCode, string brandName)
        {
            DBC.Add_Data_Brand(brandCode, brandName);
        }
        public void Data_To_Product(string productNumber, string brandCode, string availabilityLevel, string category, string productStatus, decimal price, DateTime launchDate)
        {
            DBC.Add_Data_Product(productNumber, brandCode, availabilityLevel, category, productStatus, price, launchDate);
        }
        public void Data_to_Category(string level, string name, string description, string brandcode, string categoryid)
        {
            DBC.Add_Data_Category(level, name, description, brandcode, categoryid);
        }
        public void Data_to_ProductDetail(string language, string description, string replacementsNotes, string productNumber)
        {
            DBC.Add_Data_ProductDetail(language, description, replacementsNotes, productNumber);
        }

        public List<ICategory> ReturnerCategoryFromBrand(string brandcodeval)
        {
            List<ICategory> dbcategorylist = DBC.IndlæsCategoryFraBrand(brandcodeval);
            return dbcategorylist;
        }

        public List<IProduct> ReturnerProductFromCategory(int categoryidval)
        {
            List<IProduct> dbproductlist = DBC.LoadProductFromCategory(categoryidval);
            return dbproductlist;
        }

        public List<IProductDetail> ReturnerProductdetailFromProduct(int productnumber)
        {
            List<IProductDetail> dbproductdetaillist = DBC.LoadProductdetailsFromProduct(productnumber);
            return dbproductdetaillist;
        }

        public List<IBrand> RetunerBrands()
        {
            List<IBrand> dbbrandlist = DBC.LoadBrands();
            return dbbrandlist;
        }

        public List<IProductDetail> RetunerProductDetails()
        {
            List<IProductDetail> dbproddetailList = DBC.LoadProductDetail();
            return dbproddetailList;
        }

        public List<ICategory> RetunerCategories()
        {
            List<ICategory> dbcategoryList = DBC.LoadCategories();
            return dbcategoryList;
        }

        public List<IProduct> RetunerProducts()
        {
            List<IProduct> dbproductList = DBC.LoadProducts();
            return dbproductList;
        }

        /// <summary>
        /// Indsætter store mænge data af BrandData.
        /// </summary>
        /// <param name="path"></param>
        public void InsertBulkBrandFromERP(string path)
        {
            DBconnection connector = new DBconnection();
            List<Brand> BrandList = XmlParser.ImportBrandData(path);
            DataTable table = new DataTable();

            connector.BrandBulkCopy(table);
        }
    }    
}
